<template>
  <div class="cake-wrapper">
    <div class="cake">
      <!-- 蛋糕主体 -->
      <div class="cake-body">
        <div class="layer top">
          <div class="icing"></div>
          <div class="drip"></div>
        </div>
        <div class="layer middle">
          <div class="filling"></div>
        </div>
        <div class="layer bottom">
          <div class="icing"></div>
        </div>
      </div>

      <!-- 装饰品 -->
      <div class="decorations">
        <div class="strawberries">
          <span v-for="i in 3" :key="`strawberry-${i}`">🍓</span>
        </div>
        <div class="blueberries">
          <span v-for="i in 2" :key="`blueberry-${i}`">🫐</span>
        </div>
        <div class="candles">
          <div
              v-for="i in 3"
              :key="`candle-${i}`"
              class="candle"
              :style="{
        opacity: candlesLit ? 1 : 0,
        transform: `translateY(${candlesLit ? 0 : -50}px)`,
        transitionDelay: `${i * 0.2}s`
      }"
          >
            <div class="stick"></div>
            <div class="flame-wrap">
              <div
                  class="flame"
                  :class="{ 'lit': candlesLit }"
              >
                <div class="flame-inner"></div>
              </div>
              <div
                  class="glow"
                  :class="{ 'lit': candlesLit }"
              ></div>
            </div>
          </div>
        </div>
      </div>

      <!-- 盘子 -->
      <div class="plate"></div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue';
import { useRoute } from 'vue-router';

const route = useRoute();
const candlesLit = ref(false);

/**
 * 点亮蜡烛的函数
 */
const lightCandles = () => {
  // 延迟点亮蜡烛
  setTimeout(() => {
    candlesLit.value = true;
  }, 2000);
};

// 监听路由变化
watch(
    () => route.path,
    (newPath) => {
      if (newPath === '/') { // 当路径为首页时
        lightCandles();
      } else {
        candlesLit.value = false; // 其他页面重置状态
      }
    },
    { immediate: true } // 立即执行一次
);

onMounted(() => {
  // 如果当前就在首页，直接点亮
  if (route.path === '/') {
    lightCandles();
  }
});
</script>

<style scoped>

/* 蛋糕容器 */
.cake-wrapper {
  position: relative;
  height: 175px;
  margin: 2rem 0;
  perspective: 1000px;
}

.cake {
  position: relative;
  width: 250px;
  height: 200px;
  margin: 0 auto;
  transform-style: preserve-3d;
  animation: float 3s ease-in-out infinite;
}

/* 蛋糕主体 */
.cake-body {
  position: relative;
  height: 100%;
  transform-style: preserve-3d;
}

.layer {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  border-radius: 12px;
  background: #fff5e6;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.layer.top {
  width: 180px;
  height: 60px;
  bottom: 120px;
  background: linear-gradient(to right, #ffe0e0, #ffb3b3);
}

.layer.middle {
  width: 220px;
  height: 65px;
  bottom: 60px;
  background: linear-gradient(to right, #fff0f0, #ffd6d6);
}

.layer.bottom {
  width: 250px;
  height: 70px;
  bottom: 0;
  background: linear-gradient(to right, #fff5f5, #ffe6e6);
}

/* 奶油装饰 */
.icing {
  position: absolute;
  width: 100%;
  height: 15px;
  top: -10px;
  background: rgba(255,255,255,0.9);
  border-radius: 20px;
}

.drip {
  position: absolute;
  top: 0;
  width: 100%;
  height: 20px;
  background:
      radial-gradient(circle at 10% 15%, white 20%, transparent 21%),
      radial-gradient(circle at 30% 15%, white 20%, transparent 21%),
      radial-gradient(circle at 50% 15%, white 20%, transparent 21%),
      radial-gradient(circle at 70% 15%, white 20%, transparent 21%),
      radial-gradient(circle at 90% 15%, white 20%, transparent 21%);
  background-size: 20% 100%;
  filter: drop-shadow(0 2px 2px rgba(0,0,0,0.1));
}

/* 夹心层 */
.filling {
  position: absolute;
  width: 100%;
  height: 8px;
  top: 50%;
  transform: translateY(-50%);
  background: linear-gradient(to right, #ff9999, #ff6666);
}

/* 装饰品 */
.decorations {
  position: absolute;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.strawberries {
  position: absolute;
  top: -105px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 20px;
  font-size: 24px;
}

.blueberries {
  position: absolute;
  top: -152px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 40px;
  font-size: 20px;
}

/* 蜡烛 */
.candles {
  position: absolute;
  top: -220px;
  left: 50%;
  transform: translateX(-50%);
  width: 140px;
  display: flex;
  justify-content: space-between;
}

.candle {
  position: relative;
  transform-style: preserve-3d;
  transition: all 1s ease;
}

.stick {
  width: 6px;
  height: 30px;
  background: linear-gradient(to bottom, #ffd1a1 0%, #ffaf4d 100%);
  border-radius: 4px;
}

.flame-wrap {
  position: absolute;
  top: -25px;
  left: 50%;
  transform: translateX(-50%);
  transform-style: preserve-3d;
}

/* 修改火焰相关样式 */
.flame {
  width: 6px;
  height: 21px;
  position: relative;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.flame.lit {
  opacity: 1;
  animation: flicker 1.5s ease-in-out infinite;
}

.flame-inner {
  position: absolute;
  width: 100%;
  height: 100%;
  background: linear-gradient(to bottom,
  #fff 10%,
  #ffdd1f 30%,
  #ff9933 60%,
  #ff5722 90%
  );
  border-radius: 50% 50% 20% 20% / 60% 60% 40% 40%;
  transform-origin: center bottom;
}

.glow {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 20px;
  height: 20px;
  background: radial-gradient(
      circle at center,
      rgba(255, 255, 150, 0.4) 0%,
      rgba(255, 223, 102, 0.2) 40%,
      transparent 70%
  );
  border-radius: 50%;
  opacity: 0;
  transition: opacity 0.3s ease;
  filter: blur(2px);
}

.glow.lit {
  opacity: 1;
  animation: glowFlicker 1.5s ease-in-out infinite;
}

@keyframes flicker {
  0%, 100% {
    transform: scale(1) rotate(-2deg);
  }
  25% {
    transform: scale(1.1) rotate(1deg);
  }
  50% {
    transform: scale(0.95) rotate(-1deg);
  }
  75% {
    transform: scale(1.05) rotate(2deg);
  }
}

@keyframes glowFlicker {
  0%, 100% {
    opacity: 0.5;
    transform: translate(-50%, -50%) scale(1);
  }
  50% {
    opacity: 0.7;
    transform: translate(-50%, -50%) scale(1.1);
  }
}

/* 蜡烛动画 */
.candle {
  position: relative;
  transform-style: preserve-3d;
  transition: all 0.5s ease;
}

.stick {
  width: 6px;
  height: 30px;
  background: linear-gradient(to bottom, #ffd1a1 0%, #ffaf4d 100%);
  border-radius: 4px;
}

.flame-wrap {
  position: absolute;
  top: -25px;
  left: 50%;
  transform: translateX(-50%);
  transform-style: preserve-3d;
}

.glow {
  position: absolute;
  top: -10%;
  left: -30%;
  transform: translate(-50%, -50%);
  width: 20px;
  height: 20px;
  background: radial-gradient(circle at center, rgba(255,255,150,0.4), transparent 70%);
  border-radius: 50%;
  opacity: 0;
  transition: opacity 0.3s ease;
}

/* 盘子 */
.plate {
  position: absolute;
  bottom: -20px;
  left: 50%;
  transform: translateX(-50%);
  width: 280px;
  height: 20px;
  background: linear-gradient(to bottom,
  rgba(255,255,255,0.4),
  rgba(255,255,255,0.1)
  );
  border-radius: 50%;
  filter: blur(1px);
}
</style>